//
// Created by ins on 2021/3/7.
//

#include "EnemyDao.h"
#include "MidEnemyBean.h"
#include "kisql.h"
#include <iostream>
#include <string>
#include <list>
using std::string;
using std::list;

static kisql *db;
static MYSQL_RES *result;
static MYSQL_ROW row;

EnemyDao *EnemyDao::sInstance = new EnemyDao();
EnemyDao::EnemyDao() {
    db = kisql::getInstance();
}
EnemyDao::~EnemyDao() {}
EnemyDao *EnemyDao::getInstance() {return sInstance;}

bool EnemyDao::addMidEnemy(MidEnemyBean midEnemyTemp) {
    string sql = "insert into map_guaiwu(mid, gname, glv, ghp, ggj, gfy, gbj, gxx, gexp, gyid, gmaxhp)"
                 "values(" + midEnemyTemp.getMid()+
                 midEnemyTemp.getGname()+
                 midEnemyTemp.getGlv()+
                 midEnemyTemp.getGhp()+
                 midEnemyTemp.getGgj()+
                 midEnemyTemp.getGfy()+
                 midEnemyTemp.getGbj()+
                 midEnemyTemp.getGxx()+
                 midEnemyTemp.getGexp()+
                 midEnemyTemp.getGyid()+
                 midEnemyTemp.getGmaxhp()+")";

    if(mysql_query(db->getMysql(), sql.c_str())){
        perror("mysql query err");
        return false;
    }else{ //select success
        //update insert delete
        if(mysql_field_count(db->getMysql()) == 0){
            return true;
        }
    }
    return false;
}
void EnemyDao::getMidEnemy(string nowmid, list<MidEnemyBean> &midEnemys) {

    string sql = "select * from map_guaiwu where sid = '' and mid="+nowmid;
    if(mysql_query(db->getMysql(), sql.c_str())){
        perror("mysql query err");
        return;
    } else{ //select success
        result = mysql_store_result(db->getMysql());
        if(result){
            int num_rows = mysql_num_rows(result);

            for(int i = 0; i < num_rows; i++) {
                row = mysql_fetch_row(result);
                if (row == nullptr) {
                    break;
                }
                MidEnemyBean midEnemyBean;
                midEnemyBean.setId(row[0]);
                midEnemyBean.setGname(row[1]);
                midEnemyBean.setMid(row[2]);
                midEnemyBean.setGyid(row[3]);
                midEnemyBean.setGexp(row[4]);
                midEnemyBean.setGlv(row[5]);
                midEnemyBean.setGhp(row[6]);
                midEnemyBean.setGmaxhp(row[7]);
                midEnemyBean.setGgj(row[8]);
                midEnemyBean.setGfy(row[9]);
                midEnemyBean.setGbj(row[10]);
                midEnemyBean.setGxx(row[11]);
                midEnemyBean.setSid(row[12]);

                midEnemys.push_back(midEnemyBean);
                cout << "midESize: " << midEnemys.size() << endl;
            }
        }
    }
}
void EnemyDao::getYEnemy(string id, EnemyBean &retEnemy) {
    string sql = "select * from guaiwu where id = " + id;
    if(mysql_query(db->getMysql(), sql.c_str())){
        perror("mysql query err");
        return;
    } else{ //select success
        result = mysql_store_result(db->getMysql());
        if(result){
            int num_rows = mysql_num_rows(result);

            for(int i = 0; i < num_rows; i++) {
                row = mysql_fetch_row(result);
                if (row == nullptr) {
                    break;
                }

                retEnemy.setId(row[0]);
                retEnemy.setGname(row[1]);
                retEnemy.setGinfo(row[2]);
                retEnemy.setGlv(row[3]);
                retEnemy.setGsex(row[4]);
                retEnemy.setGhp(row[5]);
                retEnemy.setGgj(row[6]);
                retEnemy.setGfy(row[7]);
                retEnemy.setGbj(row[8]);
                retEnemy.setGxx(row[9]);
                retEnemy.setGzb(row[10]);
                retEnemy.setDljv(row[11]);
                retEnemy.setDj(row[12]);
                retEnemy.setDljv(row[13]);
                retEnemy.setGyp(row[14]);
                retEnemy.setYpjv(row[15]);
            }
        }
    }
}


